package com.lvyq.qrobot;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lvyq
 * @version 1.0
 * Create by 2024-06-17 12:23
 */
public class Demo {

    public static final String tnk="Ybk_aD00p9a9LwqbLBanGD6Hetb3na5PiWloaHMXu3Y";
    public static final String i="22177190";
    public static void main(String[] args) throws InterruptedException {
    //  go();
    //addBoord();
       ta();
        // lao();

    }



    private static void addBoord() throws InterruptedException {
        while (true){
            String s = ChuanYueUtil.sendGet("http://game.qqhome.cn/mergecyhc5/instance/heal_w2.jsp?i=" + i + "&tkn=" + tnk + "&c=1&v=-1");
            Thread.sleep(1000L);
        }
    }


    public static void ta(){
        while (true){
            List<String> in = new ArrayList<>();

           // in.add("5003");//三级魂石
            in.add("5004");//四级魂石
           // in.add("5009");//小喇叭
            in.add("5010");
            in.add("5013");//小经验丹
            in.add("5014");//经验丹
            in.add("5015");//红魔晶
            in.add("5016");//紫魔晶
            //in.add("5019");//白玉令牌
            in.add("5020");//红玉令牌
            //in.add("5021");//破旧黄马褂
            in.add("5022");//御赐黄马褂
            in.forEach(ind->{
                String s = ChuanYueUtil.sendGet("http://game.qqhome.cn/mergecyhc5/herotower/monster_w2.jsp?i=22177190&tkn=Ybk_aD00p9a9LwqbLBanGD6Hetb3na5PiWloaHMXu3Y&p=" + ind);
                String text = Jsoup.parse(s).text();
                if (text.contains("你捡起了")){
                    System.out.println(ind+">>>>>"+text);
                }
                try {
                    Thread.sleep(10l);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }



    public static  void  go(){
        List<Integer> ids=  new ArrayList<>();

        String cj = "http://game.qqhome.cn/mergecyhc5/field/main_w2.jsp?i="+i+"&tkn="+tnk+"&c=mv&v=210";
        String cj2 = ChuanYueUtil.sendGet(cj);
        Document parse1 = Jsoup.parse(cj2);
        Elements links = parse1.select("a[href]");
        List<String> monsters = new ArrayList<>();
        monsters.add("夏侯惇");
        monsters.add("周瑜");
        monsters.add("于禁");
       /* monsters.add("夏侯杰");
        monsters.add("黄盖");*/
        for (Element link : links) {
            String text = link.text();

            if (monsters.contains(text)){
                System.out.println(text);
                String href = link.attr("href");
                String s = href.split("tg=")[1];
                Integer integer = Integer.valueOf(s);
                ids.add(integer);
            }
        }

        String url = "http://game.qqhome.cn/mergecyhc5/field/fight_w2.jsp?i="+i+"&tkn="+tnk+"&c=a&k=m&tg=AAAA";

        //ids.add(15642);
        // ids.add(19544);
        System.out.println("怪物数量 = " + ids.size());
        AtomicInteger index = new AtomicInteger();

        ids.forEach(id->{
            String finalUrl = url.replace("AAAA",String.valueOf(id));;

            for (int j = 0; j < 8; j++) {
               // sendGet("http://game.qqhome.cn/mergecyhc5/instance/heal_w2.jsp?i=22177190&tkn=xVKtE7ARLIoduIshHoJ6bj6Hetb3na5PiWloaHMXu3Y&c=1&v=-1");
                String s = ChuanYueUtil.sendGet(finalUrl);
                Document doc = Jsoup.parse(s);
                String s1 = doc.body().getElementsByClass("write-mood").toString();
                Document parse = Jsoup.parse(s1);

                String text = parse.text();
                if (text.startsWith("目标怪物已被杀死,请刷新再试~")){
                    System.out.println("目标怪物已被杀死,请刷新再试~");
                    break;

                }
                String[] split = text.split("气血:");
                String[] guai = split[1].split("/");
                String guai_xue = guai[0];
                String[] people = split[2].split("/");
                String person = people[0];
                String people_xue_sy = person.substring(1, person.length());

                String guai_xue_sy = guai_xue.substring(1, guai_xue.length());
                System.out.println("当前怪物剩余血量" + guai_xue_sy);

                    String  blood = "\n" +
                            "http://game.qqhome.cn/mergecyhc5/field/fight_w2.jsp?i="+i+"&tkn="+tnk+"&c=us&k=m&tg=18156&v=5039";
                    System.out.println("当前人物剩余血量" + people_xue_sy);
                try{
                    Integer   integer = Integer.valueOf(people_xue_sy);
                    if (integer<1200){
                        ChuanYueUtil.sendGet(blood);
                        System.out.println("加血");
                    }
                    }catch (Exception e){
                    String mp ="\n" +
                            "http://game.qqhome.cn/mergecyhc5/field/relive_w2.jsp?i="+i+"&tkn="+tnk+"&c=r";
                    System.out.println("喝孟婆汤,重新开始");
                    ChuanYueUtil.sendGet(mp);
                    //刷
                    ChuanYueUtil.sendGet("http://game.qqhome.cn/mergecyhc5/field/main_w2.jsp?i=" + i + "&tkn=" + tnk + "&c=mv&v=204");

                    go();
                    }
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            index.getAndIncrement();
            if (index.get() == ids.size()){
                System.out.println("打了："+index.get()+"个怪，重新加载怪物");
                go();
            }else {
                System.out.println("打了："+index.get()+"个怪,总共"+ids.size()+"个怪物");
            }
        });
    }
}
